package com.lgx.mobile.rest.topic;

import java.util.Date;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.lgx.common.exception.DataException;
import com.lgx.mobile.entity.TopicChatRecord;
import com.lgx.mobile.service.ITopicChatRecordService;

/**
 * 
 * @author xuyichen
 *
 */
@Controller
@RequestMapping("/createTopicChatRecord")
public class CreateTopicChatRecordController {
	
	private static final Logger logger = Logger.getLogger(CreateTopicChatRecordController.class);

	@Autowired
	ITopicChatRecordService topicChatRecordService;

	public ITopicChatRecordService getTopicChatRecordService() {
		return topicChatRecordService;
	}

	/**
	 * 话题聊天记录保存
	 * @param topicId topicId is key
	 * @param userId userId is key
	 * @return 0：失败；1：成功；2：参数异常，有参数为空
	 */
	@RequestMapping(value="/{topicId}/{userId}",method=RequestMethod.GET)
	@ResponseBody
	public Object createTopicChatRecord(@PathVariable("topicId") String topicId,@PathVariable("userId") String userId){
		if(StringUtils.isNotBlank(topicId)&&StringUtils.isNotBlank(userId)){
			TopicChatRecord topicChatRecord = new TopicChatRecord();
			String status = "0";
			topicChatRecord.setTopicId(Long.parseLong(topicId));
			topicChatRecord.setUserId(Long.parseLong(userId));
			topicChatRecord.setRecordDate(new Date());
			try {
				getTopicChatRecordService().createTopicChatRecord(topicChatRecord);
				status = "1";
			} catch (DataException e) {
				e.printStackTrace();
			}
			return status;
		}else{
			if(logger.isDebugEnabled()){
				logger.debug("createTopicChatRecord faild topicId and userId is not filled");
			}
//		    "createTopicChatRecord faild please check topicId and userId is filled";
			return "2";
		}
	}
	
	/**
	 * 话题聊天记录保存
	 * @param topicId topicId is key
	 * @param userId userId is key
	 * @param content content is key
	 * @return 0：失败；1：成功；2：参数异常，有参数为空
	 */
	@RequestMapping(value="/{topicId}/{userId}/{content}",method=RequestMethod.GET)
	@ResponseBody
	public Object createTopicChatRecord(@PathVariable("topicId") String topicId,@PathVariable("userId") String userId,@PathVariable("content") String content){
		if(StringUtils.isNotBlank(topicId)&&StringUtils.isNotBlank(userId)&&StringUtils.isNotBlank(content)){
			TopicChatRecord topicChatRecord = new TopicChatRecord();
			String status = "0";
			topicChatRecord.setTopicId(Long.parseLong(topicId));
			topicChatRecord.setUserId(Long.parseLong(userId));
			topicChatRecord.setRecordDate(new Date());
			topicChatRecord.setContent(content);
			try {
				getTopicChatRecordService().createTopicChatRecord(topicChatRecord);
				status = "1";
			} catch (DataException e) {
				e.printStackTrace();
			}
			return status;
		}else{
			if(logger.isDebugEnabled()){
				logger.debug("createTopicChatRecord faild topicId and userId and content is not filled");
			}
//		    "createTopicChatRecord faild please check topicId and userId and content is filled";
			return "2";
		}
	}
}
